package com.smartcruise.start.checkVerifyCode;

import android.os.CountDownTimer;

import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;

/**
 * Created by FengChaoQun
 * on 2018/3/17 0017
 */

public class VerifyPresenterImpl implements VerifyContract.Presenter {

    private VerifyContract.View mView;
    private CountDownTimer countDownTimer;
    private boolean isCount;

    public VerifyPresenterImpl(VerifyContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void start() {
        countDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mView.setSendCodeText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                mView.setSendCodeText("发送验证码");
                mView.setSendCodeEnable(true);
                isCount = false;
            }
        };
    }

    @Override
    public void clickOnSendCode() {
        if (isCount) {
            mView.showToast("验证码已经发送,请稍后重试");
        } else {
            NetCollection.sendPhoneVerifyCode(mView.getPhoneNumber(), mView, new SimpleCallBack(mView) {
                @Override
                public void onSuccess(Result result) {
                    mView.showToast("验证码发送成功");
                    countDownTimer.start();
                    mView.setSendCodeEnable(false);
                    isCount = true;
                }
            });
        }
    }

    @Override
    public void clickOnSubmit() {
        NetCollection.checkCode(mView.getPhoneNumber(), mView.getCode(), mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
                if ("true".equals(result.getData())) {
                    mView.showToast("验证成功");
                    mView.gotoSetPassword();
                } else {
                    mView.showToast("验证码错误");
                }
            }
        });
    }

    @Override
    public void destroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}
